package com.dmcbig.mediapicker.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.request.RequestOptions;
import com.dmcbig.mediapicker.PreviewActivity;
import com.dmcbig.mediapicker.R;
import com.dmcbig.mediapicker.entity.Media;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Dean on 2018/4/3.
 *
 * 预览界面缩略图adapter
 */

public class ThumbnailAdapter extends RecyclerView.Adapter<ThumbnailAdapter.MyViewHolder>{
    public static ThumbnailAdapter adapter;

    private RecyclerView recyclerView;
    private int index = 0;
    private List<Media> itemList;
    private Activity activity;
    private boolean fromPreview = true;//来自预览按钮


    public ThumbnailAdapter(boolean fromPreview, RecyclerView recyclerView,Activity activity, List<Media> itemList) {
        adapter = this;
        this.recyclerView = recyclerView;
        this.activity = activity;
        this.itemList = itemList;
        this.fromPreview = fromPreview;
    }

    //设置选中框的位置
    public void updateBox(int index) {
        this.index = index;
        if (index > 5) {
            recyclerView.smoothScrollToPosition(itemList.size()-1);
        } else if (index >= 0){
            recyclerView.smoothScrollToPosition(0);
        }
        //在滑动或者计算layout的时候不刷新数据
        if (recyclerView.getScrollState()==RecyclerView.SCROLL_STATE_IDLE
                || recyclerView.isComputingLayout()==false) {
            notifyDataSetChanged();
        }

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_thumbnail,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        Glide.with(activity).load(itemList.get(position).path)
                .apply(new RequestOptions()
                    .centerCrop()
                    .priority(Priority.IMMEDIATE))
                .into(holder.ivThumbnailItem);
        //显示当前的外部框
        if (index == position) {
            holder.ivThumbnailItem.setBackground(activity.getResources().getDrawable(R.drawable.bg_thumbnail));
        } else {
            holder.ivThumbnailItem.setBackgroundColor(0);
        }

        if (fromPreview) {
            //未选择覆盖
            if (itemList.get(position).isChoosed()) {
                holder.viewUnChoose.setVisibility(View.GONE);
            } else {
                holder.viewUnChoose.setVisibility(View.VISIBLE);
            }
        }

        holder.ivThumbnailItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            if (fromPreview) {
                PreviewActivity.previewActivity.viewpager.setCurrentItem(position,false);
            } else {
                int index = mediaIndex(itemList.get(position),PreviewActivity.previewActivity.preRawList);
                if (index >= 0) {
                    PreviewActivity.previewActivity.viewpager.setCurrentItem(index,false);
                }
            }

            }
        });
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{

        private ImageView ivThumbnailItem;
        private ImageView viewUnChoose;

        public MyViewHolder(View itemView) {
            super(itemView);
            ivThumbnailItem = (ImageView) itemView.findViewById(R.id.iv_thumbnail_item);
            viewUnChoose = (ImageView) itemView.findViewById(R.id.iv_unchoose);
        }
    }


    /**
     * 找到list中当前media的index
     * @param media
     * @return
     */
    public int mediaIndex(Media media,ArrayList<Media> list){
        int is=-1;
        if(list.size()<=0){
            return is;
        }
        for(int i=0;i<list.size();i++){
            Media m=list.get(i);
            if(m.path.equals(media.path)){
                is=i;
                break;
            }
        }
        return is;
    }
}
